import 'package:flutter/material.dart';
import '../constants/app_colors.dart';
import '../models/diary.dart';

class TagSelector extends StatefulWidget {
  final List<MoodTag> tags;
  final List<MoodTag> selectedTags;
  final Function(List<MoodTag>) onTagsSelected;

  const TagSelector({
    Key? key,
    required this.tags,
    required this.selectedTags,
    required this.onTagsSelected,
  }) : super(key: key);

  @override
  State<TagSelector> createState() => _TagSelectorState();
}

class _TagSelectorState extends State<TagSelector> {
  late List<MoodTag> _selectedTags;

  @override
  void initState() {
    super.initState();
    _selectedTags = List.from(widget.selectedTags);
  }

  @override
  void didUpdateWidget(TagSelector oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.selectedTags != widget.selectedTags) {
      _selectedTags = List.from(widget.selectedTags);
    }
  }

  void _toggleTag(MoodTag tag) {
    setState(() {
      if (_selectedTags.any((t) => t.id == tag.id)) {
        _selectedTags.removeWhere((t) => t.id == tag.id);
      } else {
        _selectedTags.add(tag);
      }
      widget.onTagsSelected(_selectedTags);
    });
  }

  @override
  Widget build(BuildContext context) {
    // 按类别分组标签
    final Map<String, List<MoodTag>> tagsByCategory = {};
    for (var tag in widget.tags) {
      if (!tagsByCategory.containsKey(tag.category)) {
        tagsByCategory[tag.category] = [];
      }
      tagsByCategory[tag.category]!.add(tag);
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '选择标签',
          style: Theme.of(context).textTheme.titleLarge,
        ),
        const SizedBox(height: 16),
        ...tagsByCategory.entries.map((entry) {
          return Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                entry.key,
                style: Theme.of(context).textTheme.titleMedium?.copyWith(
                  color: AppColors.getTagCategoryColor(entry.key),
                ),
              ),
              const SizedBox(height: 8),
              Wrap(
                spacing: 8,
                runSpacing: 8,
                children: entry.value.map((tag) {
                  final isSelected = _selectedTags.any((t) => t.id == tag.id);
                  return _buildTagChip(tag, isSelected);
                }).toList(),
              ),
              const SizedBox(height: 16),
            ],
          );
        }).toList(),
      ],
    );
  }

  Widget _buildTagChip(MoodTag tag, bool isSelected) {
    final color = AppColors.getTagCategoryColor(tag.category);
    
    return FilterChip(
      label: Text(tag.name),
      selected: isSelected,
      onSelected: (_) => _toggleTag(tag),
      backgroundColor: Colors.white,
      selectedColor: color.withOpacity(0.2),
      checkmarkColor: color,
      labelStyle: TextStyle(
        color: isSelected ? color : AppColors.textPrimary,
        fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
      ),
      side: BorderSide(
        color: isSelected ? color : Colors.grey.shade300,
      ),
    );
  }
} 